package com.qn.analyze.config.handler;

import com.qn.common.base.model.ResponseCode;
import com.qn.common.base.model.ResponseMode;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.Iterator;
import java.util.List;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ValidationExceptionHandler  {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(BindException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseMode exceptionHandler(BindException e){
        List<ObjectError> iterator = e.getAllErrors();
        StringBuffer msg = new StringBuffer();
        for (ObjectError item :  iterator ) {
            msg.append(item.getDefaultMessage());
            msg.append(";");
        }
        logger.error("BindException",e);
        return new ResponseMode(ResponseCode.ERROR.code(), msg.toString());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseMode exceptionHandler(ConstraintViolationException e){
        Iterator<ConstraintViolation<?>> iterator = e.getConstraintViolations().iterator();
        StringBuffer msg = new StringBuffer();
        while (iterator.hasNext()) {
            ConstraintViolation<?> next = iterator.next();
            msg.append(next.getMessage());
            msg.append(";");
        }
        logger.error("ConstraintViolationException",e);
        return new ResponseMode(ResponseCode.ERROR.code(), msg.toString());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseMode validatedForModleExceptionHandler(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        StringBuffer msg = new StringBuffer();
        for (FieldError fieldError : fieldErrors) {
            msg.append(fieldError.getDefaultMessage());
            msg.append(";");
        }
        logger.error("MethodArgumentNotValidException",e);
        return new ResponseMode(ResponseCode.ERROR.code(), msg.toString());
    }
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseMode missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        logger.error("MissingServletRequestParameterException",e);
        return new ResponseMode(ResponseCode.ERROR.code(), e.getLocalizedMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseMode noHandlerFoundExceptionHandler(NoHandlerFoundException e) {
        logger.error("NoHandlerFoundException",e);
        return new ResponseMode(ResponseCode.ERROR.code(), e.getLocalizedMessage());
    }

}